<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("./_inc/layoutFunction.php");

$errMsg = "";
if(sxREQ('reqtype') == "save"){
	$errMsg .= layoutIns();
}
if(sxREQ('reqtype') == ""){
	sxRDef('name', '');
	sxRDef('description', '');
	sxRDef('note', '');
	sxRDef('flags', '');
	sxRDef('type', 'form');
	sxRDef('display_only', 'N');
	sxRDef('upgrade_status', 'C');
	sxRDef('max_colspan', '20');
}

$sSQL = "SELECT m.module, m.description FROM  module m ORDER BY m.module";
$objSDAO->query($sSQL, 'losel');
$aModZ = array(''=>'-- select module');
while($row = $objSDAO->fetch('losel')) {
	$aModZ[$row['module']] = $row['module']." - ".$row['description'];
}
$sSQL = "SELECT s.sub_module, s.description FROM sub_module s "
	." WHERE s.module = '".sxRAS('module')."' ";
$sSQL .= " ORDER BY s.sub_module";
$objSDAO->query($sSQL, 'losel');
$aSubZ = array(''=>'-- select sub-module');
while($row = $objSDAO->fetch('losel')) {
	$aSubZ[$row['sub_module']] = $row['sub_module']." - ".$row['description'];
}



$aOpz = array("kMenu"=>'pro.lay', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function submCM() {
	document.objFrm.reqtype.value = 'chgMod';
	document.objFrm.submit();
}
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");

$aCmd = array("insMD"=>array("pag"=>"javascript:objFrmSubm('save')",
										"title"=>_t("save"),
										"text"=>_t("save")
										),
					"back"=>array()
					);
echo(commandsHTML($aCmd));	

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','save');

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='2'>Layout</td>" .
		"</tr>\n");
$aOptM = array('onChange'=>'submCM();');
echo("<tr>" .
			"<td class=\"labl01\" width=\"15%\">Module*</td>" .
			"<td class=\"text01\" width=\"85%\">".getHtmlSelect('module', $aModZ,'@REQ',$aOptM)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Module*</td>" .
			"<td class=\"text01\">".getHtmlSelect('sub_module', $aSubZ)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Name*</td>" .
			"<td class=\"text01\">".getHtmlText('name',22,22)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Description*</td>" .
			"<td class=\"text01\">".getHtmlText('description',80,80)."</td>" .
		"</tr>\n");
$aTypeZ = array('form'=>'form','table-line'=>'table-line');
echo("<tr>" .
			"<td class=\"labl01\">Type*</td>" .
			"<td class=\"text01\">".getHtmlSelect('type', $aTypeZ)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Max colspan*</td>" .
			"<td class=\"text01\">".getHtmlText('max_colspan',3,3)."</td>" .
		"</tr>\n");
$aYN = array('Y'=>'Yes','N'=>'No');
echo("<tr>" .
			"<td class=\"labl01\">Display only</td>" .
			"<td class=\"text01\">".getHtmlRadio('display_only',$aYN)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Flags</td>" .
			"<td class=\"text01\">".getHtmlText('flags',100)."</td>" .
		"</tr>\n");
require "../../_inc/aUpgradeStatus.php";
echo("<tr>" .
			"<td class=\"labl01\">Upgrade status</td>" .
			"<td class=\"text01\">".getHtmlText('upgrade_status',$aUpgradeStatus)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\">".getHtmlTextArea('note',100,3)."</td>" .
		"</tr>\n");
echo("</table>");
echo(commandsHTML($aCmd));
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function layoutIns($redirOnEnd = true){
	global $objSDAO;
	$errMsg = "";
	if(trim(sxREQUEST("module")) == '')
		$errMsg .= "select module<br>";
	if(trim(sxREQUEST("sub_module")) == '')
		$errMsg .= "select sub-module<br>";
	if(preg_match("/[^A-Za-z0-9_]/",sxREQUEST("name")) 
	or strlen(sxREQUEST("name")) > 22 or strlen(sxREQUEST("name")) == 0)
		$errMsg .= "name: 4 crt. A-Z a-z 0-9 _ only<br>";
	if(trim(sxREQUEST("description")) == '')
		$errMsg .= "missing description<br>";
	if(preg_match("/[^0-9]/",sxRequest("max_colspan")) 
	or intval(sxRequest("max_colspan"))< 1 or intval(sxRequest("max_colspan"))> 20)
		$errMsg .= "max colspan not numeric<br>";
	if($errMsg != ""){ 
		return $errMsg; 
	}	
	$_REQUEST['name'] = str_pad(sxREQ('name'),4,'0',STR_PAD_LEFT);
	$layout = sxREQ('module').".".sxREQ('sub_module').".".sxREQ('name');
	//existence
	$sSQL = "SELECT count(*) as num FROM layout WHERE layout='".$layout."'";
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if($row["num"] != 0)
		$errMsg .= "layout existing<br>";	
	if($errMsg != ""){ 
		return $errMsg; 
	}		
	$sSQL = "INSERT INTO " .
			"layout " .
			"(layout, module, sub_module, " .
			"description, type, " .
			"max_colspan, " .
			"flags, " .
			"display_only, " .
			"upgrade_status, " .
			"note)" .
			"VALUES('".$layout."', " .
			"'".sxRAS('module')."', '".sxRAS('sub_module')."', " .
			"'".sxRAS("description")."', '".sxRAS("type")."', " .
			sxRInt("max_colspan").", " .
			"'".sxRAS("flags")."', " .
			"'".sxRAS("display_only")."', " .
			"'".sxRAS("upgrade_status")."', " .
			"'".sxRAS("note")."'" .
			")";	
	
	$objSDAO->query($sSQL,"ins");
	
	$url = "layoutView.php?layout=".$layout;
	serverRedir($url);
	return 'abnormal end';
}
